package com.example.com.animator;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    private ImageView iv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        setContentView(R.layout.activity_main);


//找到控件
        iv = ((ImageView) findViewById(R.id.iv));

        int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        iv.measure(w, h);
        int ivheight = iv.getMeasuredHeight();

//设置由上到下以及右下到上的两个动画 , ScreenUtils是个屏幕数据帮助类，获取宽度和状态栏高度

        ObjectAnimator oa1 = ObjectAnimator.ofFloat(iv, "translationY", 0,
                ScreenUtils.getScreenHeight(this) - ScreenUtils.getStatusBarHeight(this) - ivheight);
        ObjectAnimator oa2 = ObjectAnimator.ofFloat(iv, "translationY",
                ScreenUtils.getScreenHeight(this) - ScreenUtils.getStatusBarHeight(this) - ivheight, 0);
//设置动画set
        final AnimatorSet set = new AnimatorSet();
//添加动画到set集合里面,oa2在oa1动画结束的时候开始
        set.play(oa2).after(oa1);


        set.setDuration(1000);


        set.start();
        //监听set动画
        set.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
//动画结束以后再次调用
                set.start();
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });


    }
}
